<?php 
	class Product extends CI_Controller{
		public function __construct(){
			parent::__construct();
		}
		
		public function category($str="",$sub=""){
// 			$str="sherman";
// 			$sub=0;
			$this->db->select("t2.pkid,t2.name_en,t2.img,t2.parent_pkid");
			$this->db->from("mobile_customer t1");
			$this->db->join("mobile_product_category t2", "t1.pkid=t2.customer_pkid","left");
			$this->db->where("t1.name",$str);
			$this->db->where("t2.parent_pkid",$sub);
			$res = $this->db->get()->result_array();
			$arr = array();
			foreach ($res as $P){
				$this->db->where("parent_pkid",$P['pkid']);
				$num = $this->db->count_all_results("mobile_product_category");
				if($num == 0){
					$tmp['go']			="n";
				}else{
					$tmp['go']			="y";
				}
				$tmp['pkid']			=$P['pkid'];
				$tmp['name']			=$P['name_en'];
				$tmp['img']			=$P['img'];
				$arr[] = $tmp;
			}
			echo json_encode($arr);
		}
		
		public function product_list($id){
			$arr = array();
			$this->db->select("t2.pkid,t2.name_en,t2.img,t2.price,t2.category_pkid,t1.name_en as c_name");
			$this->db->from("mobile_product_category t1");
			$this->db->join("mobile_product_item t2", "t1.pkid=t2.category_pkid");
			$this->db->where("category_pkid",$id);
			$this->db->order_by("pkid","desc");
			$res =$this->db->get()->result_array();
			foreach ($res as $P){
				$tmp['pkid']		=$P['pkid'];
				$tmp['name']		=$P['name_en'];
				$tmp['cname']		=$P['c_name'];
				$tmp['cid']			=$P['category_pkid'];
				$tmp['img']		=$P['img'];
				$tmp['price']		=$P['price'];
				$arr[] = $tmp;
			}
			echo json_encode($arr);
		}
		
		public function product_list_all($str){
			$arr = array();
			$this->db->select("pkid");
			$this->db->where("name",$str);
			$row = $this->db->get("mobile_customer")->row();
			$cid = $row->pkid;
			
			$this->db->select("t2.pkid,t2.name_en,t2.img,t2.price,t2.category_pkid,t1.name_en as c_name");
			$this->db->from("mobile_product_category t1");
			$this->db->join("mobile_product_item t2", "t1.pkid=t2.category_pkid");
			$this->db->where("t2.customer_pkid",$cid);
			$this->db->order_by("pkid","desc");
			$res =$this->db->get()->result_array();
			foreach ($res as $P){
				$tmp['pkid']		=$P['pkid'];
				$tmp['name']		=$P['name_en'];
				$tmp['cname']		=$P['c_name'];
				$tmp['cid']			=$P['category_pkid'];
				$tmp['img']		=$P['img'];
				$tmp['price']		=$P['price'];
				$arr[] = $tmp;
			}
			echo json_encode($arr);
		}
		
		public function product_detail($id="")
		{
			//$id = 90;
			$this->db->where("pkid",$id);
			$row = $this->db->get("mobile_product_item")->row();
			$tmp['name']		=$row->name_en;
			$tmp['model']	=$row->code;
			$tmp['price']		=$row->price;
			$tmp['desc']		=$row->description_en;
			$tmp['img']		=$row->img;
			$arr[] = $tmp;
			echo json_encode($arr);
		}
		
		public function listAll($str=""){
			//$str = "sherman";
			//get customer id
			$this->db->select("pkid");
			$this->db->where("name",$str);
			$row = $this->db->get("mobile_customer")->row();
			$cid = $row->pkid;
			
			//get product All
			$arr = array();
			$this->db->select("t1.name_en as cname,t2.name_en as pname");
			$this->db->from("mobile_product_category t1");
			$this->db->join("mobile_product_item t2", "t1.pkid=t2.category_pkid","left");
			$this->db->where("t2.customer_pkid",$cid);
			$this->db->order_by("t2.category_pkid","ASC");
			$res = $this->db->get()->result_array();
			foreach ($res as $P){
				$tmp['name']  = $P['cname'].'->'.$P['pname'];
				$arr[] = $tmp;
			}
			echo json_encode($arr);
		}
	}
?>